package com.joymart.aftersale.rest;


import com.joymart.aftersale.model.AfterSaleSheet;
import com.joymart.aftersale.service.AfterSaleQueryService;
import com.joymart.aftersale.service.AfterSaleService;
import com.joymart.common.model.ApprovalStatus;
import com.joymart.common.model.Range;
import com.joymart.common.utils.SecurityUtils;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
public class AfterSaleController {

    @Autowired
    private AfterSaleService afterSaleService;
    @Autowired
    private AfterSaleQueryService afterSaleQueryService;

    @GetMapping("/customer/afterSales")
    public Page<?> searchCustomerAfterSales(
            @RequestParam LocalDateTime start,
            @RequestParam LocalDateTime end,
            @RequestParam(required = false) String orderId,
            @RequestParam(required = false) ApprovalStatus status,
            @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize
            ) {
        Range range = Range.of(start, end);
        Pageable  pageable = PageRequest.of(pageNo, pageSize);
        return afterSaleQueryService.searchCustomerAfterSales(range, pageable,orderId, status);
    }

    @GetMapping("/shop/afterSales")
    public Page<?> searchShopAfterSales( @RequestParam LocalDateTime start,
                                             @RequestParam LocalDateTime end,
                                             @RequestParam(required = false) String orderId,
                                             @RequestParam(required = false) ApprovalStatus status,
                                             @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
                                             @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize) {
        Range range = Range.of(start, end);
        Pageable  pageable = PageRequest.of(pageNo, pageSize);
        return afterSaleQueryService.searchShopAfterSales(range, pageable, orderId, status);
    }

    @GetMapping("/waiting/afterSales")
    public Page<?> searchWaitingAfterSales( @RequestParam LocalDateTime start,
                                         @RequestParam LocalDateTime end,
                                         @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
                                         @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize) {
        Range range = Range.of(start, end);
        Pageable  pageable = PageRequest.of(pageNo, pageSize);
        return afterSaleQueryService.searchWaitingAfterSales(range, pageable);
    }

    @PostMapping("/afterSales")
    public void create(@RequestBody AfterSalesCreateParam afterSalesCreateParam) {
        afterSaleService.createSheet(afterSalesCreateParam.toAfterSaleSheet());
    }

    @GetMapping(value = "/afterSales/{id}" )
    public AfterSaleQueryService.AfterSaleSheetDto details(@PathVariable String id) {
        AfterSaleSheet sheet = afterSaleService.findById(id);
        SecurityUtils.checkCurrentUserIsAny(sheet::getFirstSellerId,sheet::getBuyerId,sheet::getSecondSellerId);
        return afterSaleQueryService.toDto(sheet);
    }


    @PutMapping(value = "/afterSales/{id}" ,params = "action=APPROVE")
    public void approve(@PathVariable String id, @RequestParam(required = false, defaultValue = "") String remark) {
        afterSaleService.approve(id, remark);
    }

    @PutMapping(value = "/afterSales/{id}" ,params = "action=REJECT")
    public void reject(@PathVariable String id, @RequestParam(required = false, defaultValue = "") String remark) {
        afterSaleService.reject(id, remark);
    }
    @PutMapping(value = "/afterSales/{id}" ,params = "action=CONFIRM_FIRST_SELLER_REFUNDED")
    public void confirmFirstSellerRefunded(@PathVariable String id) {
        afterSaleService.confirmFirstSellerRefunded(id);
    }
    @PutMapping(value = "/afterSales/{id}" ,params = "action=CONFIRM_SECOND_SELLER_REFUNDED")
    public void confirmSecondSellerRefunded(@PathVariable String id) {
        afterSaleService.confirmSecondSellerRefunded(id);
    }

    @Data
    public static class AfterSalesCreateParam{
        private String orderId;
        private String remarkByBuyer;
        private String imageUrl0;
        private String imageUrl1;
        private String imageUrl2;
        private String imageUrl3;
        private String imageUrl4;
        private String imageUrl5;

        public AfterSaleSheet toAfterSaleSheet(){
            AfterSaleSheet afterSaleSheet = new AfterSaleSheet();
            BeanUtils.copyProperties(this, afterSaleSheet);
            return afterSaleSheet;
        }
    }


}
